<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Post
 *
 * @author paulaan
 */

namespace Vietland\AdsBundle\Document;

use Doctrine\ODM\MongoDB\Mapping\Annotations as MongoDB;
use Symfony\Component\Validator\Constraints as Assert;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * @MongoDB\Document(db="esite", collection="ads")
 */
class Post {

    /**
     * @MongoDB\Id(strategy="increment")
     */
    protected $id;

    /**
     * @MongoDB\String
     */
    protected $content;

    /**
     * @MongoDB\String
     */
    protected $titlePost;

    /**
     * @MongoDB\String
     */
    protected $slug;

    /**
     * @MongoDB\String
     */
    protected $adress;

    /**
     * @MongoDB\String
     */
    protected $location;

    /**
     * @MongoDB\String
     */
    protected $country;

    /**
     * @MongoDB\ReferenceOne(targetDocument="Vietland\AdsBundle\Document\Category", inversedBy="posts")
     */
    protected $category;

    /**
     * @MongoDB\ReferenceOne(targetDocument="Vietland\UserBundle\Document\User", inversedBy="posts")
     */
    protected $user;
    
    /**
     *
     * @MongoDB\ReferenceMany(targetDocument="Vietland\AdsBundle\Document\Image", inversedBy="post")
     */
    protected $images;
    
    /**
     *
     * Document Manager
     */
    protected $dm;
    
    public function __construct() {
        $this->images = new ArrayCollection();
    }
    

    public function getId() {
        return $this->id;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function getContent() {
        return $this->content;
    }

    public function setContent($content) {
        $this->content = $content;
    }

    public function getTitlePost() {
        return $this->titlePost;
    }

    public function setTitlePost($title) {
        $this->titlePost = $title;
        $this->setSlug($this->convert_vi_to_en($title).'-'.time());
    }

    public function getSlug() {
        return $this->slug;
    }

    public function setSlug($slug) {
        $this->slug = $slug;
    }

    public function getAdress() {
        return $this->adress;
    }

    public function setAdress($adress) {
        $this->adress = $adress;
    }

    public function getLocation() {
        return $this->location;
    }

    public function setLocation($location) {
        $this->location = $location;
    }

    public function getCountry() {
        return $this->country;
    }

    public function setCountry($country) {
        $this->country = $country;
    }

    public function getImages($index = 0) {
        return $this->images->get($index);
    }

    public function setImages($image) {
        $this->images->add($image);
    }

    public function getCategory() {
        return $this->category;
    }

    public function setCategory($category) {
        $category = (int) $category;
        $cat = $this->dm->getRepository('VietlandAdsBundle:Category')->find($category);
        if($cat instanceof Category) $this->category = $cat;
    }

    public function getUser() {
        return $this->user;
    }

    public function setUser($user) {
        $this->user = $user;
    }
    
    public function setDm($dm){
        $this->dm = $dm;
    }
    
    public function getDm(){
        return $this->dm;
    }

    private function convert_vi_to_en($str) {
        $str = preg_replace('/(à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ)/', 'a', $str);
        $str = preg_replace('/(è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ)/', 'e', $str);
        $str = preg_replace('/(ì|í|ị|ỉ|ĩ)/', 'i', $str);
        $str = preg_replace('/(ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ)/', 'o', $str);
        $str = preg_replace('/(ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ)/', 'u', $str);
        $str = preg_replace('/(ỳ|ý|ỵ|ỷ|ỹ)/', 'y', $str);
        $str = preg_replace('/(đ)/', '', $str);
        $str = preg_replace('/(À|Á|Ạ|Ả|Ã|Â|Ầ|Ấ|Ậ|Ẩ|Ẫ|Ă|Ằ|Ắ|Ặ|Ẳ|Ẵ)/', 'A', $str);
        $str = preg_replace('/(È|É|Ẹ|Ẻ|Ẽ|Ê|Ề|Ế|Ệ|Ể|Ễ)/', 'E', $str);
        $str = preg_replace('/(Ì|Í|Ị|Ỉ|Ĩ)/', 'I', $str);
        $str = preg_replace('/(Ò|Ó|Ọ|Ỏ|Õ|Ô|Ồ|Ố|Ộ|Ổ|Ỗ|Ơ|Ờ|Ớ|Ợ|Ở|Ỡ)/', 'O', $str);
        $str = preg_replace('/(Ù|Ú|Ụ|Ủ|Ũ|Ư|Ừ|Ứ|Ự|Ử|Ữ)/', 'U', $str);
        $str = preg_replace('/(Ỳ|Ý|Ỵ|Ỷ|Ỹ)/', 'Y', $str);
        $str = preg_replace('/(Đ)/', 'D', $str);
        $str = preg_replace('/(\%)/', '0', $str);
        $str = str_replace(' ', '-', str_replace('&*#39;', '', $str));
        return $str;
    }

}